from django.shortcuts import render
# from rest_framework.filters import OrderingFilter
from django_filters import OrderingFilter
from rest_framework.generics import ListAPIView


from .serializers import SKUSerializer
from .models import SKU



class SKUListView(ListAPIView):
    """商品列表"""

    # 指定序列化器
    serializer_class = SKUSerializer
    # 指定过滤后端为排序过滤
    filter_backends = [OrderingFilter]
    # 指定以那些字段进行排序
    ordering_fields = ['create_time', 'price', 'sales']

    # 指定查询集
    def get_queryset(self):
        # 获取url路径中 正则组起别名提取的参数
        category_id = self.kwargs.get('category_id')

        # 返回的是过滤的查询集
        return SKU.objects.filter(is_launched=True, category_id=category_id)